package logic

import (
	"cartsrv/cartrpc"
	"cartsrv/internal/svc"
	"cartsrv/models/mysql"
	"cartsrv/models/redisinit"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *cartrpc.GoodsAddRequest) (*cartrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		GoodsTitle: in.GoodsTitle,
		GoodsStock: int(in.GoodsStock),
		GoodsPrice: float64(in.GoodsPrice),
		GoodsCover: in.GoodsCover,
		Status:     in.Status,
	}
	err := g.Create()
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	redisinit.SyncGoodsStock(int64(g.ID), in.GoodsStock)
	return &cartrpc.GoodsAddResponse{GoodsId: int64(g.ID)}, nil
}
